package cn.edu.nju.freshdelivery.dao.impl;

import java.util.List;

import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import cn.edu.nju.freshdelivery.dao.SellerDAO;
import cn.edu.nju.freshdelivery.model.Buyer;
import cn.edu.nju.freshdelivery.model.Seller;

/**
 * A data access object (DAO) providing persistence and search support for
 * Seller entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see cn.edu.nju.freshdelivery.dao.impl.Seller
 * @author MyEclipse Persistence Tools
 */

public class SellerDAOImpl extends HibernateDaoSupport implements SellerDAO {
	private static final Logger log = LoggerFactory
			.getLogger(SellerDAOImpl.class);
	// property constants
	public static final String USERNAME = "username";
	public static final String PASSWORD = "password";
	public static final String NAME = "name";
	public static final String ADDRESS = "address";
	public static final String LOCATION = "location";
	public static final String PHONE = "phone";
	public static final String LEAST_COST = "leastCost";
	public static final String VALID = "valid";

	protected void initDao() {
		// do nothing
	}

	public void save(Seller transientInstance) {
		log.debug("saving Seller instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Seller persistentInstance) {
		log.debug("deleting Seller instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public void update(Seller persistentInstance) {
		try {
			this.getHibernateTemplate().update(persistentInstance);
		} catch (DataAccessException e) {
			e.printStackTrace();
		}
	}

	public Seller findById(java.lang.Integer id) {
		log.debug("getting Seller instance with id: " + id);
		try {
			Seller instance = (Seller) getHibernateTemplate().get(
					"cn.edu.nju.freshdelivery.model.Seller", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Seller> findByExample(Seller instance) {
		log.debug("finding Seller instance by example");
		try {
			List<Seller> results = (List<Seller>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.edu.nju.freshdelivery.dao.impl.SellerDAO#findByProperty(java.lang.
	 * String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Seller instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Seller as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findValidByProperty(String propertyName, Object value) {
		log.debug("finding valid Seller instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from Seller as model where model."
					+ propertyName + "= ? and model.valid= 1";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("findvalic by property name failed", re);
			throw re;
		}
	}

	public List<Seller> findByUsername(Object username) {
		return findByProperty(USERNAME, username);
	}

	public List<Seller> findByPassword(Object password) {
		return findByProperty(PASSWORD, password);
	}

	public List<Seller> findByName(Object name) {
		return findByProperty(NAME, name);
	}

	public List<Seller> findByAddress(Object address) {
		return findByProperty(ADDRESS, address);
	}

	public List<Seller> findValidByAddress(Object address) {
		return findValidByProperty(ADDRESS, address);
	}

	public List<Seller> findByLocation(Object location) {
		return findByProperty(LOCATION, location);
	}

	public List<Seller> findByPhone(Object phone) {
		return findByProperty(PHONE, phone);
	}

	public List<Seller> findByLeastCost(Object leastCost) {
		return findByProperty(LEAST_COST, leastCost);
	}

	public List<Seller> findByValid(Object valid) {
		return findByProperty(VALID, valid);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.edu.nju.freshdelivery.dao.impl.SellerDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Seller instances");
		try {
			String queryString = "from Seller";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Seller merge(Seller detachedInstance) {
		log.debug("merging Seller instance");
		try {
			Seller result = (Seller) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Seller instance) {
		log.debug("attaching dirty Seller instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Seller instance) {
		log.debug("attaching clean Seller instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static SellerDAO getFromApplicationContext(ApplicationContext ctx) {
		return (SellerDAO) ctx.getBean("SellerDAO");
	}
}